<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  News Class
 *
 * @package XGO CMS v2.0
 * @subpackage News
 * @author Manh Nguyen - 30/07/2012
 * @link http://sunsoft.vn
*/
class News extends CI_Controller {

	private $page_item = 5;

	/**
	 * Contrust class
	 *
	 * @access public
	 */
	public function __construct() {
		parent::__construct();
		$this->load->library('smarty3', '', 'smarty');
                $this->smarty = new CI_Smarty3();
		$this->load->database();
		$this->load->library('datamapper');
		$this->load->library('session');
                
                $this->client_ip = getIP();

                require_once APPPATH.'modules/frontend/common/top.php';
                require_once APPPATH.'modules/frontend/common/footer.php';
                require_once (APPPATH.'modules/frontend/common/best_selling_slide.php');
                require_once (APPPATH.'modules/frontend/common/main_menu.php');
	}

	public function index() {
            $category = new Category_model();
            
            $paging['item-per-page']    = 20;
            
            $category_code              = $this->uri->rsegment(4);
            $item_code                  = str_replace(URL_SUFFIX,'',$this->uri->rsegment(5));
            
            if($category_code != "")
            {   
                $this->smarty->assign('category',$category);
                
                if($item_code != "")
                {
                    $news = new News_model();
                    $news->where('code',$item_code);
                    $news->get(1);
                    
                    //set cookie sản phẩm đã xem
                    $strip = str_replace(".", "-", $this->client_ip);
                    $itemviewed = isset($_COOKIE["$strip-itemviewed"])?$_COOKIE["$strip-itemviewed"]:"";
                    $itemviewed = str_replace(";$news->id;", "", $itemviewed);
                    $itemviewed .= ";$news->id;";
                    setcookie("$strip-itemviewed",$itemviewed, time() + 3600*24,'/svn/',$_SERVER['SERVER_NAME']);//print_r($_COOKIE);exit;
                    //end
                    
                    $news = new News_model();
                    $data['chi-tiet-tin'] = $news->get_details('', $item_code);
                    
                    $news = new News_model();
                    $data['tin-lien-quan'] = $news->get_details_relate($category_code, NULL, 5, 0, $item_code);
                    
                    $this->session->set_flashdata('redirect', $this->uri->uri_string());
                    $this->smarty->assign('data',$data);
                    $this->smarty->display('template/frontend/detail');
                }
                else
                {
                    $category = new Category_model();
                    $category->where('code',$category_code);
                    $category->get(1);
                    //print_r($category->id);exit;
                    $news = new News_model();
                    $data['item'] = $news->get_by_category_code("$category_code", $paging['item-per-page'], 1);
                    $paging['total-item'] = $news->count_by_category(array("$category->id"));
                    $paging['cur-page'] = 1;
                    
                    $this->session->set_flashdata('redirect', $this->uri->uri_string());
                    $this->smarty->assign(
                                            array(
                                                'category_code'=>$category_code,
                                                'data'=>$data,
                                                'paging'=>$paging,
                                                'type'=>"$category_code"
                                                    )
                                          );
                    $this->smarty->display('template/frontend/list');
                }
            }
            
            
	}
        
}

/* End of file news.php */
/* Location: ./system/application/controllers/news.php */